import type { App } from "vue";
import { createRouter, createWebHashHistory, type RouteRecordRaw } from "vue-router";
export const Layout = () => import("@/layout/index.vue");

// 静态路由

export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "控制台",
          icon: "homeFilled",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "profile",
        name: "Profile",

        component: () => import("@/views/profile/index.vue"),
        meta: { title: "个人中心", icon: "user", hidden: true },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },
  {
    path: "/category", // 直接作为根路径
    name: "Category",
    component: Layout, // 使用Layout组件
    children: [
      {
        path: "index", // 子路由（可选）
        component: () => import("@/views/category/index.vue"),
        name: "CategoryList",
        meta: { title: "分类列表", icon: "menu" },
      },
    ],
  },
  {
    path: "/course",
    name: "Course",
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/course/index.vue"),
        name: "CourseList",
        meta: { title: "基础课程", icon: "checked" },
      },
    ],
  },
  {
    path: "/practical-course",
    name: "PracticalCourse",
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/practical-course/index.vue"),
        name: "PracticalCourseList",
        meta: { title: "实战课程", icon: "management" },
      },
    ],
  },
  {
    path: "/userInfo",
    name: "UserInfo",
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/userInfo/index.vue"),
        name: "UserInfoList",
        meta: { title: "用户信息", icon: "avatar" },
      },
    ],
  },
  {
    path: "/interview-guide",
    name: "InterviewGuide",
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/interview-guide/index.vue"),
        name: "InterviewGuideList",
        meta: { title: "面试指南", icon: "promotion" },
      },
    ],
  },
  {
    path: "/community",
    name: "Community",
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/community/index.vue"),
        name: "CommunityList",
        meta: { title: "社区动态", icon: "comment" },
      },
    ],
  },
  {
    path: "/article",
    name: "Article",
    component: Layout,
    children: [
      {
        path: "index",
        component: () => import("@/views/article/index.vue"),
        name: "ArticleList",
        meta: { title: "精品文章", icon: "list" },
      },
    ],
  },
  {
    path: "/:pathMatch(.*)*", // 正则匹配所有未被定义的路径
    redirect: "/404", // 跳转到 404 页面
    meta: { hidden: true },
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export function setupRouter(app: App<Element>) {
  app.use(router);
}

export default router;
